<template>
  <el-dialog :title="title" v-if="open" v-model="open" width="1300px" append-to-body>
    <el-form ref="form" :model="form" :rules="rules" label-width="90px" :inline="true" @submit.native.prevent>
      <el-form-item label="查询条件:" prop="goodsName">
        <el-input v-model="queryParams.goodsName" placeholder="请输入商品名称" @keyup.enter="handleQuery"></el-input>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" @click="handleQuery">搜索</el-button>
        <el-button @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>
    <div class="content">
      <div v-show="tableHeight && showTree" :style="`height: ${tableHeight}px;position: relative;`">
        <div class="left" :style="`height: ${tableHeight}px;`">
          <div class="left-top">
            <div class="">商品分类</div>
            <div class="">
              <img style="width: 30px; height: 30px" @click="handleEdge" :src="showTree ? imgs[1] : imgs[0]" />
            </div>
          </div>
          <el-tree
            ref="classifyTreeRef"
            class="mt-2"
            node-key="id"
            :data="categoryList"
            :props="{ label: 'classifyName', children: 'children' }"
            :expand-on-click-node="false"
            :filter-node-method="filterNode"
            highlight-current
            default-expand-all
            @node-click="itemClick"
          />
        </div>
      </div>
    <el-card class="box-card" shadow="always" ref="table">
      <el-table ref="table" v-loading="loading" :data="dataSource" @selection-change="handleSelectionChange"
        @row-click="rowClick" @row-dblclick="dbclick" :row-key="getRowKey">
        <el-table-column type="selection" width="55" align="center" :selectable="selectHandle"
          :reserve-selection="true" />
        <el-table-column label="#" type="index" width="55" align="center"> </el-table-column>
        <el-table-column label="商品图片" align="center" width="120">
          <template #default="scope">

            <ImagePreview :width="100" :height="100" :src="scope.row.packImg" :preview-src-list="[scope.row.packImg]" />
          </template> </el-table-column>>

        <el-table-column label="商品名称" align="left" prop="goodsName" min-width="200" />
        <el-table-column label="商品分类" align="center" prop="classifyName" width="150">
          <template #default="scope">
            {{ scope.row.classifyName }}
          </template>
        </el-table-column>

        <!-- <el-table-column label="商品来源" align="center" prop="goodsType" width="150">
          <template #default="scope">
            {{ selectDictLabel(goods_source, scope.row.goodsType) }}
          </template>
        </el-table-column> -->
        <el-table-column label="商品品牌" align="center" prop="brandName" />
        <!-- <el-table-column label="商品规格" align="center" prop="goodsSize" /> -->
        <el-table-column label="代发" align="center" prop="supplyMode">
          <template #default="scope">
            {{ selectDictLabel(drop_shipping, scope.row.supplyMode) }}
          </template>
        </el-table-column>
        <el-table-column label="上架状态" align="center" width="200">
          <template #default="scope">
            {{ selectDictLabel(material_audit_status, scope.row.auditStatus || '') }}
          </template>
        </el-table-column>
        <el-table-column label="集采成本价" align="center" prop="costPrice" />
        <el-table-column label="批发价" align="center" prop="wholesalePrice" />
      </el-table>

      <div class="fixed-icon" @click="handleEdge" v-show="!showTree">
      <el-icon>
        <DArrowRight />
      </el-icon>
    </div>
    </el-card>

    </div>
    <template #footer>
      <div class="dialog-footer">
        <div class="footer">
      <pagination v-show="total > 0" :total="total" v-model:page.sync="queryParams.pageNum"
        v-model:limit.sync="queryParams.pageSize" @pagination="getList" />
    </div>
        <div class="flex">
          <el-button @click="cancel">取 消</el-button>
        <el-button type="primary" @click="submit">确定</el-button>
        </div>
      </div>
    </template>
  </el-dialog>
</template>

<script>
import listMixin from '@/mixins/listMixin';
import ImagePreview from '@/components/ImagePreview/index.vue';
import zk from '@/assets/images/zhankai.svg';
import zd from '@/assets/images/zhedie.svg';
export default {
  name: "selectMaterialForm",
  mixins: [listMixin],
  components: {
    ImagePreview
  },
  data() {
    return {
      open: false,
      title: "选择商品",
      loading: false,
      goods_source: [],
      drop_shipping: [],
      material_audit_status: [],//商品审核状态
      imgs: [zk, zd],
      showTree:true,

      queryParams: {//查询条件
        pageNum: 1,
        pageSize: 10,
        auditStatus: 1,
      },
      urls: {
        list: '/order/goods/list',
        delete: '/order/goods',
      },
      tableHeight: 0,
      tableDefHeight: 0,
    }
  },
  computed: {
    tableDefHeight(){
      this.$nextTick(() => {
        console.log('document.documentElement.clientHeight :>> ', document.documentElement.clientHeight);
        if (this.dataSource.length < 10) {
          return document.documentElement.clientHeight - 480 + 'px';
        } else {
          return 'auto';
        }
      });
    }
  },
  methods: {
    show() {
      this.open = true;
      this.getDicts(['goods_source', 'material_audit_status', 'drop_shipping'])
      this.getList()
      this.getCommonCategoryAllList()
      this.$nextTick(() => {
        setTimeout(() => {
          this.tableHeight = this.$refs.table.$el.scrollHeight;
        }, 200);
      });
    },
    submit() {
      if(!this.selectedRows.length) return this.$message.warning('请选择商品')
      this.$emit('ok', this.selectedRows)
      this.open = false;
    },
    
    dbclick(row) {
          this.$emit('ok', [row])
          this.open = false;
        },
    cancel() {
      this.open = false;
    },
    getRowKey(row) {
      return row.id;
    },
    rowClick(row, column, event) {
      this.$refs.table.toggleRowSelection(row);
    },
    handleEdge() {
      this.showTree = !this.showTree;
    },
    itemClick(data) {
      this.currentIndex = data.id;
      console.log('this.currentIndex :>> ', this.currentIndex);
      this.categoryList.forEach((data) => {
        data.active = false;
      });
      data.active = !data.active;
      this.queryParams.classifyId = data.id;
      this.getList();
    },
  }
}
</script>

<style lang="scss" scoped>
.left {
  width: 240px;
  border: 1px solid #e6ebf5;
  border-radius: 4px;
  overflow-x: hidden;
  overflow-y: auto;
  padding-bottom: 60px;
  margin-right: 10px;
  background-color:#fff;
  border-radius: 12px;

  .fodder_list {
    z-index: 20;

    .fodder_item {
      display: flex;
      justify-content: space-between;
      align-items: center;
      padding: 10px 20px;
      cursor: pointer;

      .box {
        display: flex;
        justify-content: flex-start;
        align-items: center;

        svg {
          margin-right: 10px;
        }

        .fodder_name {
          font-size: 14px;
          color: #303133;
          width: 84px;
          white-space: nowrap;
          overflow: hidden;
          text-overflow: ellipsis;
        }
      }
    }

    .fodder_item.active,
    .fodder_item:hover {
      background-color: #f5f5f5;
    }
  }
}

.left-top {
  display: flex;
  justify-content: space-between;
  padding: 6px 20px;
  border-bottom: 1px solid #e6ebf5;
  align-items: center;

  .box {
    display: flex;
    justify-content: flex-start;
    align-items: center;
    font-size: 24px;
    color: #303133;
    font-weight: bold;
  }
}
.fixed-icon {
  position: fixed;
  //left: 0;
  top: 400px;
  z-index: 999;
  background-color: #409eff;
  padding: 12px 2px;
  color: #fff;
  border-top-left-radius: 4px;
  border-bottom-left-radius: 4px;
  display: flex;
  cursor: pointer;
}

.content {
  display: flex;
  justify-content: space-between;
  position: relative;
}

.w-tree {
  width: calc(100% - 260px);
}

.w-all {
  width: 100%;
}
.box-card{
  position: relative;
  min-height: 500px;
}
.footer{
  width: 100%;
}
.dialog-footer{
  display: flex;
  align-items: center;
  justify-content: space-between;
}
</style>
<style>
.footer .pagination-container{

}
</style>